package com.thenie.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thenie.chat.mapper.FriendMapper;
import com.thenie.chat.pojo.Friend;
import com.thenie.chat.service.IFriendService;
import com.thenie.chat.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import static com.thenie.chat.controller.DialogSocketHandler.SESSION_MAP;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thenie
 * @since 2022-05-22
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements IFriendService {

    @Autowired
    private FriendMapper friendMapper;

    /*
        查找我的好友列表
     */
    @Override
    public List<UserInfoVo> findMyFriendList(long id) {

        return friendMapper.findMyFriendList(id);
    }
    /**
     * 查找我的好友列表，根据是否在线排序
     * @param id
     * @return
     */
    public List<UserInfoVo> findMyFriendListOrderByOnLine(long id){
        List<UserInfoVo> myFriendList = friendMapper.findMyFriendList(id);
        //根据在线状态排序
        Iterator<UserInfoVo> iterator = myFriendList.iterator();
        while(iterator.hasNext()){
            UserInfoVo userInfoVo = iterator.next();
            if (SESSION_MAP.containsKey(userInfoVo.getId().toString())){
                userInfoVo.setOnLine(1);
            }else{
                userInfoVo.setOnLine(0);
            }
        }
        myFriendList.sort(new Comparator<UserInfoVo>() {
            @Override
            public int compare(UserInfoVo o1, UserInfoVo o2) {
                return o2.getOnLine()-o1.getOnLine();
            }
        });
        return myFriendList;
    }
    /**
     * 判断是否是我的好友
     * @param fromId
     * @param targetId
     * @return
     */
    @Override
    public boolean isMyFriend(long fromId,long targetId){
        if (fromId == targetId){
            return true;
        }
        Friend friend = friendMapper.selectOne(new QueryWrapper<Friend>().select("id")
                .eq("aid", fromId).eq("bid", targetId)
                .or()
                .eq("aid", targetId).eq("bid", fromId));
        if (friend != null){
            return true;
        }

        return false;
    }

    /**
     * 创建好友
     * @param fromId
     * @param toId
     * @return
     */
    @Override
    public int createFriend(long fromId, long toId) {
        Friend friend = new Friend();
        friend.setAid(fromId);
        friend.setBid(toId);
        int ok = friendMapper.insert(friend);
        return ok;
    }

    /**
     * 删除好友
     * @param fromId 发起方用户id
     * @param toId 被删除用户id
     * @return 更改数据库条数
     */
    @Override
    public int removeFriend(long fromId,long toId){
        return friendMapper.deleteMyFriend(fromId,toId);
    }

}
